package com.sorhis.vo;


import com.sorhis.common.constant.Constants;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应信息主体
 */
@Data
public class ResultVO<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /** 成功 */
    public static final int SUCCESS = Constants.SUCCESS;

    /** 失败 */
    public static final int FAIL = Constants.FAIL;

    private int code;

    private String msg;

    private T data;

    public static <T> ResultVO<T> ok()
    {
        return restResult(null, SUCCESS, null);
    }

    public static <T> ResultVO<T> ok(T data)
    {
        return restResult(data, SUCCESS, "操作成功");
    }

    public static <T> ResultVO<T> ok(T data, String msg)
    {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> ResultVO<T> fail()
    {
        return restResult(null, FAIL, null);
    }

    public static <T> ResultVO<T> fail(String msg)
    {
        return restResult(null, FAIL, msg);
    }

    public static <T> ResultVO<T> fail(T data)
    {
        return restResult(data, FAIL, null);
    }

    public static <T> ResultVO<T> fail(T data, String msg)
    {
        return restResult(data, FAIL, msg);
    }

    public static <T> ResultVO<T> fail(int code, String msg)
    {
        return restResult(null, code, msg);
    }

    private static <T> ResultVO<T> restResult(T data, int code, String msg) {
        ResultVO<T> apiResult = new ResultVO<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
